package com.whjfxx.terminal.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by bo.chen in 2020-02-12
 */
public class ValidateUtils {

    /**
     * 日期正则
     */
    public final static String DATETIME_PATTERN = "\\d{4}(\\-|\\/|.)\\d{1,2}\\1\\d{1,2}";

    /**
     * 日期格式 yyyyMMddHHmmss校验
     */
    public final static String DATETIME_YYYYMMDDHHMMSS_PATTERN = "(\\d{4})([0|1]\\d{1})([0|1|2|3]\\d{1})([0|1|2]\\d{1}\\d{2}\\d{2})";

    /**
     * 正整数正则
     */
    public final static String INTEGER_PATTERN = "[1-9]\\d*";

    /**
     * 正整数正则
     */
    public final static String ZERO_AND_INTEGER_PATTERN = "0|([1-9]\\d*)";

//    /**
//     * 保留3位小数正则
//     */
//    public final static String FLOAT_3_PATTERN = "(^[1-9](\\d+)?(\\.\\d{1,3})?$)|(^0$)|(^\\d\\.\\d{1,3}$)";
//
//    /**
//     * 金额正则
//     */
//    public final static String DECIMAL_PATTERN = "(^[1-9](\\d+)?(\\.\\d{1,3})?$)|(^0$)|(^\\d\\.\\d{1,3}$)";

    /**
     * 单价正则（最多保留8位小数）
     */
    public final static String PRICE_PATTERN = "(^[1-9](\\d+)?(\\.\\d{1,8})?$)|(^0$)|(^\\d\\.\\d{1,8}$)";


    /**
     * 身份证号码正则
     */
    public final static String IDCARD_PATTERN = "\\d{17}[\\d|X]|\\d{15}";


    public final static String TRIM_ESC_TAB = "\\s*|\\t|\\r|\\n";

    /**
     * 大写字母加数字
     */
    public final static String CODE_PATTERN = "^[0-9A-Z]+$";

    /**
     * ip正则
     */
    public final static String IP_PATTERN = "(localhost)|(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";

    /**
     * 手机号码正则
     */
    public final static String PHONE_PATTERN = "0?(13|14|15|18)[0-9]{9}";

    /**
     * 单价值正则（例：100.001元/个）
     */
    public final static String PRICE_VALUE_PATTERN = "((^[1-9](\\d+)?(\\.\\d{1,8})?)|(^0)|(^\\d\\.\\d{1,8}))\\元/\\D$";

    /**
     * 验证零和正整数
     */
    public static boolean validPositiveInteger(String integer) {
        if (isEmpty(integer)) {
            return false;
        }
        return regular(integer,ZERO_AND_INTEGER_PATTERN);
    }
    /**
     * 验证正整数
     *
     * @param integer
     * @return
     */
    public static boolean valiInteger(String integer) {
        if (isEmpty(integer)) {
            return false;
        }
        return regular(integer,INTEGER_PATTERN);
    }

    /**
     * 验证时间格式
     *
     * @param dateTime
     * @return
     */
    public static boolean valiDateTime(String dateTime) {
        if (isEmpty(dateTime)) {
            return false;
        }
        return regular(dateTime,DATETIME_PATTERN);
    }



    /**
     * 执行正则表达式验证
     *
     * @param str
     *            被验证的字符串
     * @param regular
     *            正则表达式
     * @return
     */
    protected static boolean regular(String str, String regular) {
        if (isEmpty(str) || isEmpty(regular)) {
            return false;
        }
        Pattern pattern = Pattern.compile(regular);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    /**
     * 判断字符串是否为空
     *
     * @param s
     * @return
     */
    protected static boolean isEmpty(String s) {
        return (null == s || s.length() == 0);
    }


    /**
     * 去空格、回车、制表符
     * @param source
     * @return
     */
    public static String trimEscTab(String source) {
        if(StringUtils.isBlank(source)) {
            return null;
        }
        return source.replaceAll(TRIM_ESC_TAB,"");
    }

    /**
     * 校验仓库，库区，货架编码是否合法
     * @param code
     * @return
     */
    public static boolean checkCodeLegal(String code){
        return regular(code, CODE_PATTERN);
    }


    /**
     * 验证ip地址
     *
     * @param ip	ip
     * @return
     */
    public static boolean valiIp(String ip) {
        if (StringUtils.isBlank(ip)) {
            return false;
        }
        return regular(ip,IP_PATTERN);
    }

    /**
     * 验证手机号码
     *
     * @param phone	手机号码
     * @return
     */
    public static boolean valiPhone(String phone) {
        if (StringUtils.isBlank(phone)) {
            return false;
        }
        return regular(phone,PHONE_PATTERN);
    }

    private static final int[] WI = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };

    private static final char[] VALI_CODE = {'1', '0','X', '9', '8', '7', '6', '5', '4', '3', '2'};

    private static final int Y = 11;

    /**
     * 校验身份证
     * @param idCard
     * @return
     */
    public static boolean idCard(String idCard) {
        if (isEmpty(idCard)) {
            return false;
        }
        if( regular(idCard,IDCARD_PATTERN) ){
            char[] idCharArr = idCard.toCharArray();
            char lastCode = idCharArr[idCharArr.length - 1];
            int s = 0 , i = 0 , len  = idCharArr.length;
            for(; i < len - 1 ; ++i){
                s += (Integer.parseInt(String.valueOf(idCharArr[i])) * Integer.parseInt(String.valueOf(WI[i])));
            }
            if(lastCode == VALI_CODE[s % Y]){
                return true;
            }
        }
        return false;
    }

    /**
     * 单价校验
     * @param price
     * @return
     */
    public static boolean validPrice(String price) {
        if (StringUtils.isBlank(price)) {
            return false;
        }
        return regular(price,PRICE_PATTERN);
    }

    /**
     * 单价值校验
     * @param priceValue
     * @return
     */
    public static boolean validPriceValue(String priceValue) {
        if (StringUtils.isBlank(priceValue)) {
            return false;
        }
        return regular(priceValue,PRICE_VALUE_PATTERN);
    }

    /**
     * 判断number1 是否是 number2的整数倍
     * @param number1
     * @param number2
     * @return
     */
    public static boolean validIntMul(int number1, int number2) {
        if (0 == number2) {
            return false;
        }
        return 0 == number1 % number2;
    }


    public static void main(String[] args) {
        boolean f = regular("19921221120534", DATETIME_YYYYMMDDHHMMSS_PATTERN);
        System.out.println(f);
    }
}
